package com.cloudkinto.service.consumer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cloudkinto.common.RabbitQueueConstant;
import com.cloudkinto.entity.RakutenMarkRecordDo;
import com.cloudkinto.service.consumer.vo.amazon.AmazonConsumerReq;
import com.cloudkinto.service.consumer.vo.amazon.AmazonItemConsumerReq;
import com.cloudkinto.service.consumer.vo.amazon.PlatStoreConsumerReq;
import com.cloudkinto.service.consumer.vo.amazon.fulfillment.AmazonMarkShipConsumerReq;
import com.cloudkinto.service.consumer.vo.amazon.inventory.AmazonStockToPlatReq;
import com.cloudkinto.service.consumer.vo.orderSyncRecord.PlatformOrderSyncRecordReq;
import com.cloudkinto.service.platform.PlatformOrderAmazonSyncService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;

@Slf4j
@Component
@RequiredArgsConstructor
public class PlatformOrderAmazonConsumer {
    private final PlatformOrderAmazonSyncService platformOrderAmazonSyncService;
    private final RedisTemplate redisTemplate;
    private final RabbitTemplate rabbitTemplate;

    /**
     * 同步订单表 历史
     * @param msg
     */

    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformStore_History_Amazon))
    public void storeSyncOrderHistory(String msg) {
        PlatStoreConsumerReq req = JSONObject.parseObject(msg, PlatStoreConsumerReq.class);
        String s = platformOrderAmazonSyncService.updateAmazonOrdersHistory(req.getStoreId(), req.getOrderNumbers());
        if (StringUtils.isNotBlank(s)) {
            log.error(s);
        }
    }

    /**
     * 同步订单表
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformOrder_Amazon))
    public void syncOrderAmazonCm(String msg) {
        AmazonConsumerReq req = JSONObject.parseObject(msg, AmazonConsumerReq.class);
        try {
            platformOrderAmazonSyncService.consumerUpdateAmazonItems(req);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformStore_Amazon))
    public void storeSyncOrder(String msg) {
        PlatStoreConsumerReq req = JSONObject.parseObject(msg, PlatStoreConsumerReq.class);
        String s = null;
        try {
            s = platformOrderAmazonSyncService.updateAmazonOrders(req);
        }
        catch (Exception e) {
            s = e.getMessage();
        }
        if (StringUtils.isNotBlank(s)) {
            rabbitTemplate.convertAndSend(RabbitQueueConstant.PlatformOrder_SyncRecord_Update, JSON.toJSONString(new PlatformOrderSyncRecordReq(req.getSyncId(), 3, new Date(), null, s, null)));
        }
    }


    /**
     * 标发
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformStore_Amazon_MarkShip))
    public void markShip(String msg) {
        AmazonMarkShipConsumerReq req = JSONObject.parseObject(msg, AmazonMarkShipConsumerReq.class);
        try {
            platformOrderAmazonSyncService.makeShip(req);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }



    /**
     * 像乐天索取 标发状态
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformOrder_Amazon_MarkStatus))
    public void getMarkStatus(String msg) {
        if (StringUtils.isBlank(msg)) {
            return;
        }
        RakutenMarkRecordDo req = JSONObject.parseObject(msg, RakutenMarkRecordDo.class);
        try {
            platformOrderAmazonSyncService.getFeed(req);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 库存回传
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.StockToPlatform_Amazon))
    public void setStock(String msg) {
        AmazonStockToPlatReq req = JSONObject.parseObject(msg, AmazonStockToPlatReq.class);
        platformOrderAmazonSyncService.uploadFeedsForInventory(req.getStockList(), req.getStoreDo(), req.getUserId());
    }



    /**
     * 同步平台商品
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.GetPlatformProduct_Amazon))
    public void getItem(String msg) {
        AmazonItemConsumerReq req = JSONObject.parseObject(msg, AmazonItemConsumerReq.class);
        try {
            platformOrderAmazonSyncService.syncListingsItems(req.getStoreDo(), req.getPackageHierarchySku());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
